
app.controller("landCtrl", ['$scope', '$window', '$stateParams', '$timeout', '$http', '$state', '$cookieStore', 'Md5', function ($scope, $window, $stateParams, $timeout, $http, $state, $cookieStore, Md5) {
    //    设置页面title
    console.log($stateParams.title)
    $window.document.title = $stateParams.title;

    // 房东登录
    if ($cookieStore.get('landId')) {
        $http({
            method: 'post',
            url: $scope.httptit + '/index/landlord/lst.html',
            data: {
                id: $cookieStore.get("landId")
            },
            dataType: 'json',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest: $scope.changeData
        }).then(function successCallback(response) {
            $scope.RecData = response.data.data
            $scope.mount_money = response.data.mount_money;
            console.log($scope.RecData)
        }, function errorCallback(response) {
            console.log(response)
            // 请求失败执行代码
        });
        $state.go('landLits');
    } else if ($stateParams.title == '找回密码') {
        console.log('ss')
    } else {
        $state.go('loginLand');
    }

    $scope.openLand = function () {
        if ((/^1(3|4|5|7|8)\d{9}$/.test($scope.cleanAccount))) {
            $http({
                method: 'post',
                url: $scope.httptit + '/index/landlord/login.html',
                data: {
                    phone: $scope.cleanAccount,
                    password: $scope.cleanPassWord
                },
                dataType: 'json',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },

                transformRequest: $scope.changeData
            }).then(function successCallback(response) {
                console.log(response.data)
                if (response.data.status == 101) {
                    // $cookieStore.put('cleanId',Md5.hex_md5(response.data.data))//加密
                    $cookieStore.put('landId', response.data.data)//加密
                    console.log(response.data)
                    // 登录成功请求加载数据
                    $http({
                        method: 'post',
                        url: $scope.httptit + '/index/landlord/lst.html',
                        data: {
                            id: response.data.data
                        },
                        dataType: 'json',
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                        transformRequest: $scope.changeData
                    }).then(function successCallback(response) {
                        $scope.RecData = response.data.data
                        console.log($scope.RecData)
                    }, function errorCallback(response) {
                        console.log(response)
                        // 请求失败执行代码
                    });
                    $state.go('landLits');
                }else if (response.data.status == 102) {
                    $scope.cleanAccount = ''
                    $scope.cleanPassWord = ''
                    $.alert({
                        title: '登录',
                        text: '账号或密码错误，请重新登录',

                    });
                }else{
                    $scope.cleanAccount = ''
                    $scope.cleanPassWord = ''
                    $.alert({
                        title: '提示',
                        text: response.data.data,

                    });
                }
            }, function errorCallback(response) {
                if (response.data.status == 103) {
                    $.alert({
                        title: '登录',
                        text: response.data.data,
                        onOK: function () {
                            //点击确认
                        }
                    });
                }
                // 请求失败执行代码
            });
        } else {
            $scope.cleanAccount = ''
            $scope.cleanPassWord = ''
            $.alert({
                title: '登录',
                text: '手机号输入错误，请重新输入',
                onOK: function () {

                    //点击确认
                }
            });
        }

    }



    $scope.landTX = function () {
        $.confirm({
            title: '提现',
            text: '是否提现',
            onOK: function () {
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/wx/withdraw.html',
                    data: {
                        landlordid: $cookieStore.get("landId")
                    },
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: $scope.changeData
                }).then(function successCallback(response) {
                    if (response.data.status == 101) {
                        $scope.userData.money = 0;
                        $.alert({
                            title: '提示',
                            text: '提现成功'
                        });
                    } else if (response.data.status == 102) {
                        $.alert({
                            title: '提示',
                            text: '提现失败',
                        });
                    } else if (response.data.status == 103) {
                        $.alert({
                            title: '提示',
                            text: response.data.data,

                        });
                    }

                }, function errorCallback(response) {
                    console.log(response)
                    // 请求失败执行代码
                });
            }
        })

    }
    $scope.OnClick = '';
    $scope.getCode = function () {
        if ((/^1(3|4|5|7|8)\d{9}$/.test($scope.backPhone))) {
            if ($scope.OnClick) {
                console.log($scope.OnClick)
                return
            }
            $scope.OnClick = 'disabled';
            $timeout(function () {
                $scope.OnClick = '';
            }, 60000);
            //上传号码到服务器接收验证码
            $http({
                method: 'post',
                url: $scope.httptit + '/index/user/sendChit.html',
                data: {
                    userid: $cookieStore.get('login').id,
                    tel: $scope.backPhone,
                },
                dataType: 'json',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                transformRequest: $scope.changeData
            }).then(function successCallback(response) {
                console.log(response)
                $cookieStore.put('VERCode', response.data.data)
                if (response.data.status == 101) {
                    // 弹出框提示
                    $.prompt({
                        title: '验证码',
                        text: '请输入接收到的验证码',
                        input: '请输入验证码',
                        empty: false, // 是否允许为空
                        onOK: function (input) {
                            //验证成功后的操作   
                            if (input == $cookieStore.get('VERCode')) {
                                $http({
                                    method: 'post',
                                    url: $scope.httptit + '/index/user/checkChit.html',
                                    data: {
                                        userid: $cookieStore.get('login').id,
                                        tel: $scope.backPhone,
                                    },
                                    dataType: 'json',
                                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                                    transformRequest: $scope.changeData
                                }).then(function successCallback(response) {
                                    console.log(response.data)
                                    if (response.data.status == 101) {
                                        $.alert({
                                            title: '验证',
                                            text: '验证成功',
                                            onOK: function () {
                                                //点击确认
                                                $scope.backBTN = true;
                                            }
                                        });
                                    } else {
                                        $.alert({
                                            title: '验证',
                                            text: response.data.data,

                                        });
                                    }
                                }, function errorCallback(response) {
                                    console.log(response)
                                    // 请求失败执行代码
                                });

                            } else {
                                $.alert({
                                    title: '验证',
                                    text: '验证失败,请重新输入',
                                    onOK: function () {
                                        //点击确认
                                    }
                                });
                            }

                            //点击确认
                        },
                        onCancel: function () {

                        }
                    });
                } else {
                    $.alert({
                        title: '验证',
                        text: response.data.data.message,

                    });
                }

            }, function errorCallback(response) {
                console.log(response)
                // 请求失败执行代码
            });

        } else {
            // 弹出框提示
            $.alert({
                title: '短信验证',
                text: '手机号码输入错误，请重新输入',
                onOK: function () {
                    //点击确认
                }
            });
        }


    }

    $scope.backPass = function () {
        if (!$scope.backPhone) {
            $.alert({
                title: '提示',
                text: '请输入手机号码',
            });
        } else if (!$scope.backBTN) {
            $.alert({
                title: '提示',
                text: '请验证手机号码',
            });
        } else if (!$scope.backPassword) {
            $.alert({
                title: '提示',
                text: '请输入修改密码',
            });
        } else {


            $http({
                method: 'post',
                url: $scope.httptit + '/index/landlord/reset.html',
                data: {
                    phone: $scope.backPhone,
                    password: $scope.backPassword,
                },
                dataType: 'json',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                transformRequest: $scope.changeData
            }).then(function successCallback(response) {
                console.log(response.data)
                if (response.data.status == 101) {
                    $.alert({
                        title: '提示',
                        text: '修改成功',
                        onOK: function () {
                            //点击确认
                            $state.go('loginLand');
                        }
                    });
                } else {
                    $.alert({
                        title: '验证',
                        text: response.data.data,

                    });
                }
            }, function errorCallback(response) {
                console.log(response)
                // 请求失败执行代码
            });
        }


    }

}]);
